package gaml.testing;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import gaml.error.GamlException;
import gaml.text.YamlLine;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.junit.Test;


public class YamlLineTest {
	
	static final String fullSample = "\t\t\t &anc full line value #full line node comment";
	static final String noAnchorSample = "\t\tno anchor line node value #no anchor line node comment";
	static final String noCommentSample = "\t\t\t &anc1 no comment line value";
	static final String minimalSample = "minimal line value";
	
	static final String tagSample = "\t!taglinetype &taglineanc tag line value #tag line comment";
	static final String tagSampleNoAnchor = "\t!taglinetype  tag line value #tag line comment";
	static final String tagSampleNoComment = "\t!taglinetype &taglineanc tag line value";
	
	@Test
	public void testFullSample () throws GamlException {
		YamlLine line = new YamlLine (fullSample, "\t", 0);
		
		assertEquals (3, line.getIdent ());
		assertEquals ("anc", line.getAnchor ());
		assertEquals ("full line value", line.getValue ());
	}
	
	@Test
	public void testNoAnchorSample () throws GamlException {
		YamlLine line = new YamlLine (noAnchorSample, "\t", 1);
		
		assertEquals (1, line.getIdent ());
		assertEquals ("", line.getAnchor ());
		assertEquals ("no anchor line node value", line.getValue ());
	}
	
	@Test
	public void testNoCommentSample () throws GamlException {
		YamlLine line = new YamlLine (noCommentSample, "\t", 1);
		
		assertEquals (2, line.getIdent ());
		assertEquals ("anc1", line.getAnchor ());
		assertEquals ("no comment line value", line.getValue ());
	}
	
	@Test
	public void testMinimalSample () throws GamlException {
		YamlLine line = new YamlLine (minimalSample, "\t", 0);
		
		assertEquals (0, line.getIdent ());
		assertEquals ("", line.getAnchor ());
		assertEquals ("minimal line value", line.getValue ());
	}
	
	@Test
	public void testTag () {
		StringBuilder rx = new StringBuilder ();
		
		rx.append ("^\t*\\s*") // identation
			.append ("(?:!(\\S+)(?:\\s*))?") // optional tag
			.append ("(?:&(\\S+)(?:\\s*))?") // optional anchor
			.append ("(.*?)") // value
			.append ("(?:#(.*))?$"); // optional comment
		
		Matcher matcher = Pattern.compile (rx.toString ()).matcher (tagSample);
		assertTrue (matcher.find ());
		
		assertEquals ("taglinetype", matcher.group (1));
		assertEquals ("taglineanc", matcher.group (2));
		assertEquals ("tag line value", matcher.group (3).trim ());
		assertEquals ("tag line comment", matcher.group (4));
		
		matcher = Pattern.compile (rx.toString ()).matcher (tagSampleNoAnchor);
		assertTrue (matcher.find ());
		
		assertEquals ("taglinetype", matcher.group (1));
		assertEquals (null, matcher.group (2));
		assertEquals ("tag line value", matcher.group (3).trim ());
		assertEquals ("tag line comment", matcher.group (4));
		
		matcher = Pattern.compile (rx.toString ()).matcher (tagSampleNoComment);
		assertTrue (matcher.find ());		
		
		assertEquals ("taglinetype", matcher.group (1));
		assertEquals ("taglineanc", matcher.group (2));
		assertEquals ("tag line value", matcher.group (3).trim ());
		assertEquals (null, matcher.group (4));
	}
}
